package com.portal.back.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.portal.back.service.ISysMessageService;
import com.portal.base.dao.BaseDao;
import com.portal.base.dao.IBaseDao;

import com.portal.domain.SysContent;
import com.portal.domain.SysMessage;
import com.portal.util.common.DateTimeUtil;
/**
 * @Title:留言类service
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2010-3-25
 * 
 */
public class SysMessageService implements  ISysMessageService{
	private IBaseDao<SysMessage, Integer> dao;

	public SysMessageService() {
		dao = new BaseDao<SysMessage, Integer>(SysMessage.class);
	}
	public  List<SysMessage>  getAllMessage(){
		String  sql="select * from  sys_message";
		 List<SysMessage>  list=dao.findList(sql);
		 if(list!=null && list.size()>0){
			 List<SysMessage> newlist=new  ArrayList<SysMessage>();
			 for(int i=0;i<list.size();i++){
				 SysMessage  message=list.get(i);
				 if("1".equals(message.getType()))message.setTypeString("业务咨询 ");
				 if("2".equals(message.getType()))message.setTypeString("问题处理");
				 String content=message.getContent();
				 if(content!=null && !"".equals(content) &&  content.length()>8)message.setContent(content.substring(0,8)+"...");
				 newlist.add(message);
			 }
			 return   newlist;
		 }
		return  null;
	}
	 public  boolean   deleteById(String idString){
		  boolean isFlag = true;
			if (idString == null || "".equals(idString))
				return false;
			String sql = "delete from sys_message where  id=?";
			String[] delteid = idString.split("_");
			try{
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from sys_message where  id="+delteid[0];
				dao.delete(sql);
			}
			}catch (Exception e) {
				e.printStackTrace();
				return  false;
			}
			return isFlag;
	  }
	 public  SysMessage getById(String id){
		  String  sql="select * from sys_message where  id=?";
		    List<SysMessage> list=dao.findList(sql, new Object[]{id});
		    if(list!=null &&  list.size()>0){
		    	  SysMessage  message=list.get(0);
		    	  if("1".equals(message.getType()))message.setTypeString("业务咨询 ");
					 if("2".equals(message.getType()))message.setTypeString("问题处理");
	   			  return  message;
		    }
		    return  null;
	  }
	public void saveMessage(SysMessage message) {
		dao.saveEntity(message);
	}
}
